package com.fingard.dsp.bank.directbank.ceb02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IFinanceRecInfo;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.ReqQryFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 还款信息查询
 * @author F
 *
 */
public class CEB02QryFinanceRecInfo extends CEB02Base implements IFinanceRecInfo{

	@Override
	public RetAtsFinanceRecInfo qryFinanceRecInfo(ReqQryFinanceRecInfo recInfo) throws Exception {
		RetAtsFinanceRecInfo retAts = new RetAtsFinanceRecInfo(recInfo.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
        String tmpRespInfo = "";
		int PAGE = 0;
		int BISHU = 50;
        try {
        	for(int i=0;i<=PAGE;i++) {
            CEB02Header bankHeader = new CEB02Header();
            //银行获取相关信息
            bankHeader.FTranCode = "G051";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader_3(xmlWriter, bankHeader);   
            //写入body
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("CHANNEL_CODE", bankFront.org);//接入机构号
            xmlWriter.writeElementString("DEALER_CODE",recInfo.DealerCode);//接入机构号
            xmlWriter.writeElementString("QISHI", i*BISHU+"");//接入机构号
            xmlWriter.writeElementString("BISHU", BISHU+"");//接入机构号
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),bankHeader);//发送银行
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();
                        
                //xml规范去头去尾
                //String retxml = tmpStrRet[1].substring(6, tmpStrRet[1].length()-16);
                
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                
                if(tmpBkReader != null){
                	String tmpRetCode = tmpBkReader.getFirstText("/Out/Head/ErrCode");
                	String tmpRetMsg = tmpBkReader.getFirstText("/Out/Body/ErrorInfo");
                        if (tmpRetCode.length()==0) {
                        	tmpRespCode = "0";
                        	tmpRespInfo = "获取成功";
                          //翻页处理
                            if(PAGE == 0) {
                                int Z_BISHU = Integer.valueOf(tmpBkReader.getFirstText("/Out/Body/Z_BISHU")); 
                                int count = Z_BISHU/BISHU;
                                if(count>0) {
                                	if(Z_BISHU%BISHU==0) {
                                		PAGE = count - 1;
                                	} else {
                                	PAGE = count;
                                	}
                                }
                            }
                            while (tmpBkReader.read("/Out/Body/Frame")) {
                            	RetAtsFinanceRecInfoDetail tmpRetDetail = new RetAtsFinanceRecInfoDetail();
                                tmpRetDetail.DealerCode = recInfo.DealerCode;
                                tmpRetDetail.VIN = tmpBkReader.readedXElement.getSingleText("DJ_NO");
                                tmpRetDetail.RepaymentDate = tmpBkReader.readedXElement.getSingleText("REPAY_DATE");
                                tmpRetDetail.RepaymentMoney = tmpBkReader.readedXElement.getSingleText("REPAY_AMOUNT");
                                String CarMortgageTime = tmpBkReader.readedXElement.getSingleText("PI_RUTIME");
                                if(StringHelper.isNullOrEmpty(CarMortgageTime)){
                                	tmpRetDetail.CarMortgageTime = "";
                                }else {
                                tmpRetDetail.CarMortgageTime = CarMortgageTime.substring(0, 10).replace("-", "");
                                }
                                tmpRetDetail.Reserve1 = "";
                                tmpRetDetail.Reserve2 = "";
                                tmpRetDetail.Reserve3 = "";
                                tmpRetDetail.Reserve4 = "";
                                tmpRetDetail.Reserve5 = "";
                                retAts.writeRetDetail(tmpRetDetail);  
                             }
                        } else {
                        	tmpRespCode = "-1";
                            tmpRespInfo = tmpRetMsg;
                            break ;
                        }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                    break ;
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
                break ;
            }
         }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }

}
